package com.it.park.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.common.JsonObj;
import com.it.park.common.MD5;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.rms.client.model.User;
import com.it.park.rms.client.service.UserService;

/**
 * @author Administrator
 *账号管理
 */
@Controller
@RequestMapping("/user/account")
public class AccountController {
	private String rootPath=Constant.USER_PATH;
	@Autowired
	private UserService userService;
	/**
	 * 邮箱设置 ,是否订阅
	 */
	@RequestMapping("accountManage")
	public String accountManage(HttpServletRequest request,HttpSession session){
		User user = userService.selectByPrimaryKey(SessionUtil.getUserID(session));
		request.setAttribute("user", user);
		return rootPath+"account/accountManage";
	}
	
	/**
	 * 个人资料修改页面
	 */
	@RequestMapping("personData")
	public String personData(HttpServletRequest request,HttpSession session){
		return rootPath+"account/personData";
	}
	
	/**
	 * 个人资料修改页面
	 */
	@RequestMapping("personDataSave")
	@ResponseBody
	public JsonObj personDataSave(User user,HttpServletRequest request,HttpSession session){
		JsonObj obj = new JsonObj();
		//判断界面传过来的用户名和登陆者是否匹配
		User u = SessionUtil.getSessionUser(session);
		user.setId(u.getId());
		if(!user.getUserName().equals(u.getUserName())){
			obj.setMessage(Constant.IS_NOT_EQUAL_LOGINNAME);
			obj.setSuccess(false);
			return obj;
		}
		boolean success =  userService.updateByPrimaryKeySelective(user);
		if(success){
			obj.setMessage(Constant.SUCCESS_MSG);
		}else{
			obj.setMessage(Constant.FAIL_MSG);
		}
		obj.setSuccess(success);
		return obj;
	}
	
	/**
	 *消息设置页面
	 */
	@RequestMapping("messageSet")
	public String messageSet(HttpSession session,HttpServletRequest request){
		User user = userService.selectByPrimaryKey(SessionUtil.getUserID(session));
		request.setAttribute("user", user);
		return  rootPath+"account/messageSet";
	}
	
	/**
	 *消息保存页面
	 */
	@RequestMapping("messageSave")
	@ResponseBody
	public JsonObj messageSave(User user,HttpSession session,HttpServletRequest request){
		JsonObj obj = new JsonObj();
		user.setId(SessionUtil.getUserID(session));
		boolean success =  userService.updateByPrimaryKeySelective(user);
		if(success){
			obj.setMessage(Constant.SUCCESS_MSG);
		}else{
			obj.setMessage(Constant.FAIL_MSG);
		}
		obj.setSuccess(success);
		return obj;
	}
	
	/**
	 *修改密码页面
	 */
	@RequestMapping("updatePwd")
	public String updatePwd(HttpSession httpSession,HttpServletRequest request){
		return  rootPath+"account/updatePwd";
	}
	
	/**
	 *修改密码保存
	 */
	@RequestMapping("updatePwdSave")
	public String updatePwdSave(User user_,HttpSession session,HttpServletRequest request){
		User user = (User) session.getAttribute("user");
		String pwd = MD5.md5(user_.getPassword());
		user.setPassword(pwd);
		
		if(!user.getUserName().equals(user_.getUserName())){
			request.setAttribute("msg", Constant.IS_NOT_EQUAL_LOGINNAME);
			return rootPath+"account/updatePwd";
		}
		
		boolean success =  userService.updatePwd(user);
		if(success){
			//修改成功，清楚session，必须重登
			session.invalidate();
			return"redirect:/login";
		}else{
			request.setAttribute("msg", "修改密码失败!");
			return rootPath+"account/updatePwd";
		}
	}
	
	
}
